<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;

class LoginController extends Controller
{
    //login
    public function login()
    {
    	//3 模板 login.blade.php 的路径
    	//4. 在模板文件中改路径,该表单输入,提交地址
    	return view('admin.login.login');
    	// return 111;
    }

    // dologin
    public function dologin(Request $request)
    {
    	// dd(session() -> all());
    	$scode = session('milkcaptcha');
    	// dd($request -> all());
    	$rcode = $request -> input('captcha');

    	if($scode != $rcode)
    	{
    		return back() -> with(['info' => '验证码错误']);
    	}
    	$this -> validate($request,[
    			'email' => 'required|email',
    			'password' => 'required'
    		],[
    			'eamil.required' => '邮箱不能为空',
    			'eamil.email' => '邮箱必须合法',
    			'password.required' => '密码不能为空'
    		]);
    	$data = $request -> except('_token');

    	$res = DB::table('manager') -> where('email',$data['email']) -> first();
    	// var_dump(empty($res));
    	if(!$res)
    	{
    		return back() -> with(['info' => '邮箱不存在错误']);
    	}
    	$password = Crypt::decrypt($res -> password);
    	if($password != $data['password'])
    	{
    		return back() -> with(['info' => '密码错误']);

    	}
    	session(['master' => $res]);
    	return redirect('/admin/index') -> with(['info' => '登录成功']);
    	// dd($password);
    }

    public function loginout()
    {
    	// 清除session中的master的值
    	session() -> forget('master');
    	// session() -> flush(); 清空session
    	// dd(session() -> all());
    	return redirect('/admin/login') -> with(['info' => '您以安全退出!!']);
    }
}
